'use strict';

const FS = require('fs');
const PATH = require('path');
const { optimize, extendDefaultPlugins } = require('svgo');
const filepath = 'D:\\MyTemp\\testsvg\\in\\symbol\\plus.svg';
const config = {
  multipass: true, // boolean. false by default
  js2svg: {
    indent: 2, // string with spaces or number of spaces. 4 by default
    pretty: true, // boolean, false by default
  },
  plugins: extendDefaultPlugins([
    'convertStyleToAttrs',
    {
      name: 'removeUselessStrokeAndFill',
      active: false
    },
    {
      name: 'customPluginName',
      type: 'perItem', // 'perItem', 'perItemReverse' or 'full'
      params: {
      },
      fn: (item, params, info) => {
        if (item && item.attributes) {
          for (const [name, value] of Object.entries(item.attributes)) {
            if (name == 'fill' || name == 'stroke') {
              delete item.attributes[name];
            }
          }
        }
      }
    }
  ])
};

FS.readFile(filepath, 'utf8', function(err, data) {
  if (err) {
    throw err;
  }

  const result = optimize(data, { path: filepath, ...config });

  console.log(result.data);

  // {
  //     // optimized SVG data string
  //     data: '<svg width="10" height="20">test</svg>'
  //     // additional info such as width/height
  //     info: {
  //         width: '10',
  //         height: '20'
  //     }
  // }
});